<template>
  <div>
    <el-row type="flex" class="row-bg btn-row" justify="end">
      <el-button size="mini" @click="add" type="primary" icon="el-icon-plus">新增公告
      </el-button>
    </el-row>
    <el-table class="el-calendar-table" :data="listData.items" v-loading.body="listLoading" size="mini" stripe border
      fit highlight-current-row>
      <el-table-column prop="id" label="ID" width="50">
      </el-table-column>
      <el-table-column prop="title" label="公告标题" width="150">
      </el-table-column>
      <el-table-column prop="context" label="公告内容" min-width="320">
        <template slot-scope="scope"><div class="ellipsis">{{scope.row.context}}</div></template>
      </el-table-column>
      <el-table-column prop="start_at" label="生效时间" width="160">
      </el-table-column>
      <el-table-column width="160" prop="end_at" label="结束时间">
      </el-table-column>
      <el-table-column width="100" prop="pkg" label="包名">
      </el-table-column>
      <el-table-column width="100" prop="chn" label="渠道">
      </el-table-column>
      <el-table-column width="100" prop="system" label="手机系统">
      </el-table-column>
      <el-table-column width="160" prop="created_at" label="创建时间">
      </el-table-column>
      <el-table-column label="操作" width="130" fixed="right">
        <template slot-scope="scope">
          <div>
            <el-button type="primary" size="mini" @click="update(scope.row)">编辑</el-button>
            <el-button type="success" size="mini" @click="deleteRow(scope.row)">删除</el-button>
          </div>
        </template>
      </el-table-column>
    </el-table>
    <div class="pagination">
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="q.page_no"
        :page-sizes="$store.state.app.pageSizeArr" :page-size="q.page_size"
        layout="total, sizes, prev, pager, next, jumper" :total="listData.total">
      </el-pagination>
    </div>
    <save-update v-if="showUpdate" :showUpdate.sync="showUpdate" :id="show_id"></save-update>
  </div>
</template>

<script>
import saveUpdate from './saveUpdate.vue'
import { getBulletinList, deleteBulletinById } from '@/api'

export default {
  name: "index",
  components: { saveUpdate },
  data () {
    return {
      show_id: null,
      listData: {},
      q: { page_size: 30, page_no: 1 },
      listLoading: false,
      showUpdate: false
    }
  },
  created () {
    this.getList()
  },
  methods: {
    add () {
      this.show_id = null
      this.showUpdate = true
    },
    update (row) {
      this.show_id = row.id
      this.showUpdate = true
    },
    deleteRow (row) {
      this.$confirm(`确定删除【${row.id}】吗？`, '是否继续', {
        type: 'warning',
        confirmButtonText: '确定',
        cancelButtonText: '取消'
      }).then(() => {
        deleteBulletinById(row.id).then(res => {
          if (res.status === 0) {
            this.$message({ type: 'success', message: '删除成功' })
            this.getList()
          }
        })
      }).catch(() => {
        this.$message({ type: 'info', message: '已取消' })
      })
    },
    query () {
      this.getList()
    },
    reload () {
      this.getList()
    },
    handleSizeChange (val) {
      this.q.page_size = val
      this.getList()
    },
    handleCurrentChange (val) {
      this.q.page_no = val
      this.getList()
    },

    getList () {
      getBulletinList().then(res => {
        if (res.status === 0) {
          this.listData = res.data
        }
      })
    },
  },
  watch: {
    showUpdate () {
      if (!this.showUpdate) {
        this.getList()
      }
    }
  }
}
</script>

<style scoped>
</style>
